home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Utilities / md5 / Source / md5drivr.c < prev   
Encoding:
C/C++ Source or Header  |  1994-03-01  |  5.7 KB  |  201 lines

  1. /*
  2.  ***********************************************************************
  3.  ** md5driver.c -- sample test routines                               **
  4.  ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
  5.  ** Created: 2/16/90 RLR                                              **
  6.  ** Updated: 1/91 SRD                                                 **
  7.  ***********************************************************************
  8.  */
  9.  
  10. /*
  11.  ***********************************************************************
  12.  ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.  **
  13.  **                                                                   **
  14.  ** RSA Data Security, Inc. makes no representations concerning       **
  15.  ** either the merchantability of this software or the suitability    **
  16.  ** of this software for any particular purpose.  It is provided "as  **
  17.  ** is" without express or implied warranty of any kind.              **
  18.  **                                                                   **
  19.  ** These notices must be retained in any copies of any part of this  **
  20.  ** documentation and/or software.                                    **
  21.  ***********************************************************************
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #include <time.h>
  27. #include <string.h>
  28. #include "md5.h"
  29.  
  30. /* Prints message digest buffer in mdContext as 32 hexadecimal digits.
  31.    Order is from low-order byte to high-order byte of digest.
  32.    Each byte is printed with high-order hexadecimal digit first.
  33.  */
  34. static void MDPrint (mdContext)
  35. MD5_CTX *mdContext;
  36. {
  37.   int i;
  38.  
  39.   for (i = 0; i < 16; i++)
  40.     printf ("%02x", mdContext->digest[i]);
  41. }
  42.  
  43. /* size of test block */
  44. #define TEST_BLOCK_SIZE 500000
  45.  
  46. /* number of blocks to process */
  47. #define TEST_BLOCKS 10
  48.  
  49. /* number of test bytes = TEST_BLOCK_SIZE * TEST_BLOCKS */
  50. static long TEST_BYTES = (long)TEST_BLOCK_SIZE * (long)TEST_BLOCKS;
  51.  
  52. /* A time trial routine, to measure the speed of MD5.
  53.    Measures wall time required to digest TEST_BLOCKS * TEST_BLOCK_SIZE
  54.    characters.
  55.  */
  56. static void MDTimeTrial ()
  57. {
  58.   MD5_CTX mdContext;
  59.   time_t endTime, startTime;
  60.   unsigned char data[TEST_BLOCK_SIZE];
  61.   unsigned int i;
  62.  
  63.   /* initialize test data */
  64.   for (i = 0; i < TEST_BLOCK_SIZE; i++)
  65.     data[i] = (unsigned char)(0);
  66.  
  67.   /* start timer */
  68.   printf ("MD5 time trial. Processing %ld characters...\n", TEST_BYTES);
  69.   time (&startTime);
  70.  
  71.   /* digest data in TEST_BLOCK_SIZE byte blocks */
  72.   MD5Init (&mdContext);
  73.   for (i = TEST_BLOCKS; i > 0; i--)
  74.     MD5Update (&mdContext, data, TEST_BLOCK_SIZE);
  75.   MD5Final (&mdContext);
  76.  
  77.   /* stop timer, get time difference */
  78.   time (&endTime);
  79.   MDPrint (&mdContext);
  80.   printf (" is digest of test input.\n");
  81.   printf
  82.     ("Seconds to process test input: %ld\n", (long)(endTime-startTime));
  83.   printf
  84.     ("Characters processed per second: %ld\n",
  85.      TEST_BYTES/(endTime-startTime));
  86. }
  87.  
  88. /* Computes the message digest for string inString.
  89.    Prints out message digest, a space, the string (in quotes) and a
  90.    carriage return.
  91.  */
  92. static void MDString (inString)
  93. char *inString;
  94. {
  95.   MD5_CTX mdContext;
  96.   unsigned int len = strlen (inString);
  97.  
  98.   MD5Init (&mdContext);
  99.   MD5Update (&mdContext, inString, len);
  100.   MD5Final (&mdContext);
  101.   MDPrint (&mdContext);
  102.   printf (" \"%s\"\n", inString);
  103. }
  104.  
  105. /* Computes the message digest for a specified file.
  106.    Prints out message digest, a space, the file name, and a carriage
  107.    return.
  108.  */
  109. static void MDFile (filename)
  110. char *filename;
  111. {
  112.   FILE *inFile = fopen (filename, "rb");
  113.   MD5_CTX mdContext;
  114.   int bytes;
  115.   unsigned char data[1024];
  116.  
  117.   if (inFile == NULL) {
  118.     printf ("%s can't be opened.\n", filename);
  119.     return;
  120.   }
  121.  
  122.   MD5Init (&mdContext);
  123.   while ((bytes = fread (data, 1, 1024, inFile)) != 0)
  124.     MD5Update (&mdContext, data, bytes);
  125.   MD5Final (&mdContext);
  126.   MDPrint (&mdContext);
  127.   printf (" %s\n", filename);
  128.   fclose (inFile);
  129. }
  130.  
  131. /* Writes the message digest of the data from stdin onto stdout,
  132.    followed by a carriage return.
  133.  */
  134. static void MDFilter ()
  135. {
  136.   MD5_CTX mdContext;
  137.   int bytes;
  138.   unsigned char data[16];
  139.  
  140.   MD5Init (&mdContext);
  141.   while ((bytes = fread (data, 1, 16, stdin)) != 0)
  142.     MD5Update (&mdContext, data, bytes);
  143.   MD5Final (&mdContext);
  144.   MDPrint (&mdContext);
  145.   printf ("\n");
  146. }
  147.  
  148. /* Runs a standard suite of test data.
  149.  */
  150. static void MDTestSuite ()
  151. {
  152.   printf ("MD5 test suite results:\n");
  153.   MDString ("");
  154.   MDString ("a");
  155.   MDString ("abc");
  156.   MDString ("message digest");
  157.   MDString ("abcdefghijklmnopqrstuvwxyz");
  158.   MDString
  159.     ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  160.   MDString
  161.     ("1234567890123456789012345678901234567890\
  162. 1234567890123456789012345678901234567890");
  163.   /* Contents of file foo are "abc" */
  164.   MDFile ("foo");
  165. }
  166.  
  167. int main (argc, argv)
  168. int argc;
  169. char *argv[];
  170. {
  171.   int i;
  172.  
  173.   /* For each command line argument in turn:
  174.   ** filename          -- prints message digest and name of file
  175.   ** -sstring          -- prints message digest and contents of string
  176.   ** -t                -- prints time trial statistics for 10M
  177.                           characters
  178.   ** -x                -- execute a standard suite of test data
  179.   ** (no args)         -- writes messages digest of stdin onto stdout
  180.   */
  181.   if (argc == 1)
  182.     MDFilter ();
  183.   else
  184.     for (i = 1; i < argc; i++)
  185.       if (argv[i][0] == '-' && argv[i][1] == 's')
  186.         MDString (argv[i] + 2);
  187.       else if (strcmp (argv[i], "-t") == 0)
  188.         MDTimeTrial ();
  189.       else if (strcmp (argv[i], "-x") == 0)
  190.         MDTestSuite ();
  191.       else MDFile (argv[i]);
  192.  
  193.   return(0);
  194. }
  195.  
  196. /*
  197.  ***********************************************************************
  198.  ** End of md5driver.c                                                **
  199.  ******************************** (cut) ********************************
  200.  */
  201.